/*
 * Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
 */
package com.pitop.succedaneum.service;

import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.SuccedaneumMaterialException;
import com.pitop.succedaneum.api.SuccedaneumMaterialFeignApi;
import com.pitop.succedaneum.entity.SuccedaneumMaterial;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 可替代料材
 *
 * @author berton
 */
@Service("succedaneumMaterialSVImpl")
@Slf4j
public class SuccedaneumMaterialSVImpl {

    @Autowired
    private SuccedaneumMaterialFeignApi succedaneumMaterialFeignApi;

    /**
     * 加载一个对象SuccedaneumMaterial
     *
     * @param id id* @param code 编码
     * @return SuccedaneumMaterial
     */
    public SuccedaneumMaterial load(Long id, String code) {
        if (id == null && code == null) {
            throw new SuccedaneumMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return succedaneumMaterialFeignApi.load(id, code);
    }

    /**
     * 加载一个对象SuccedaneumMaterial 通过id
     *
     * @param id id
     * @return SuccedaneumMaterial
     */
    public SuccedaneumMaterial loadById(Long id) {
        if (id == null) {
            throw new SuccedaneumMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return succedaneumMaterialFeignApi.loadById(id);
    }

    /**
     * 加载一个对象SuccedaneumMaterial 通过code
     *
     * @param code 编码
     * @return SuccedaneumMaterial
     */
    public SuccedaneumMaterial loadByCode(String code) {
        if (code == null) {
            throw new SuccedaneumMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return succedaneumMaterialFeignApi.loadByCode(code);
    }


    /**
     * 删除对象SuccedaneumMaterial
     *
     * @param id   id*
     * @param code 编码
     */

    public void delete(Long id, String code) {
        if (id == null && code == null) {
            throw new SuccedaneumMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        succedaneumMaterialFeignApi.delete(id, code);
    }


    /**
     * 查询SuccedaneumMaterial分页
     *
     * @param succedaneumMaterial 对象
     * @param curPage             当前页
     * @param pageSize            分页条数
     * @return List<SuccedaneumMaterial>
     */
    public List<SuccedaneumMaterial> list(SuccedaneumMaterial succedaneumMaterial, int curPage, int pageSize) {
        return succedaneumMaterialFeignApi.list(succedaneumMaterial, curPage, pageSize);
    }

    /**
     * 查询SuccedaneumMaterial分页
     *
     * @param id           id*
     * @param code         编码
     * @param materialCode
     * @param curPage      当前页
     * @param pageSize     分页条数   @return List<SuccedaneumMaterial>
     */
    public List<SuccedaneumMaterial> list(Long id, String code, String materialCode, int curPage, int pageSize) {
        if (curPage < 0) {
            curPage = 1;
        }

        if (pageSize < 0) {
            pageSize = Page.limit;
        }

        SuccedaneumMaterial succedaneumMaterial = new SuccedaneumMaterial();
        succedaneumMaterial.setId(id);
        succedaneumMaterial.setCode(code);
        succedaneumMaterial.setMaterialCode(materialCode);

        return succedaneumMaterialFeignApi.list(succedaneumMaterial, curPage, pageSize);
    }


    public int count(Long id, String code) {
        return succedaneumMaterialFeignApi.count(id, code);
    }


    public int count(SuccedaneumMaterial succedaneumMaterial) {
        return succedaneumMaterialFeignApi.count(succedaneumMaterial);
    }


    /**
     * 保存
     * 1.验证数据合法性
     * 2.保存可替换材料
     *
     * @param succedaneumMaterial 实体
     * @throws BaseException
     */
    public void save(SuccedaneumMaterial succedaneumMaterial) {
        //1.验证数据合法性
        if (succedaneumMaterial == null) {
            throw new SuccedaneumMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        //2.保存可替换材料
        succedaneumMaterialFeignApi.build(succedaneumMaterial);
    }

    /**
     * 更新
     * 1.验证数据合法性
     * 2.更新可替换材料
     *
     * @param succedaneumMaterial 实体
     * @throws BaseException
     */
    public void modify(SuccedaneumMaterial succedaneumMaterial) {
        //1.验证数据合法性
        if (succedaneumMaterial == null) {
            throw new SuccedaneumMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        //2.更新可替换材料
        succedaneumMaterialFeignApi.modify(succedaneumMaterial);
    }

}
